//微信登录
exports.wxLogin = function(req, res) {

	//获取restful参数
	var restfulParams = req.routeInfo.restfulParams;

	//code
	var code = restfulParams.code;
	var linshiOpenid = restfulParams.loginstate;
	console.log('--------------------------')
	console.log(linshiOpenid)

	//最终结果
	var resultData = {};

	//任务流创建
	async.waterfall([

		//获取openid
		function(cb) {
			var openidUrl = "http://"+config.publicConfig.SERVER_IP+"/weixinDemo/getUserAccessToken.php?code=" + code;
//			var openidUrl = "http://demo2.artmofang.com/weixinDemo/getUserAccessToken.php?code=" + code;
			R.SEND_HTTP(
				req, {
					url: openidUrl,
					method: 'GET'
				},
				function(err, data) {

					console.log('获取openid');
					console.log(data);

					if(err) {
						return RES.response(res, false, "获取openid失败");
					}

					resultData.openIds = data;

					cb();

				}
			);

		},
		//拉取用户资料
		function(cb) {

			//如果是临时登录则不拉用户资料
			if(linshiOpenid == 'linshi') {
				//console.log('这是一个临时openid操作')
				cb();
				return;
			}

			//console.log('开始拉取用户资料')

			var infoUrl = "http://"+config.publicConfig.SERVER_IP+"/weixinDemo/getWeChatUserInfo.php";
//			var infoUrl = "http://demo2.artmofang.com/weixinDemo/getWeChatUserInfo.php";

			R.SEND_HTTP(
				req, {
					url: infoUrl,
					method: 'POST',
					data: {
						"user_access_token": resultData.openIds.access_token,
						"openid": resultData.openIds.openid
					}
				},
				function(err, data) {

					console.log('拉取用户资料')
					console.log(data)

					//判断是否有错误信息
					if(err) {
						return cb(err, data);
					}

					resultData.userInfo = data;

					cb();
				}
			);

		},
		//是否关注
		function(cb) {

			var attentionParams = {
				openid: resultData.openIds.openid
			}

			R.SEND_HTTP(
				req, {
					url: config.publicConfig.MIDDLE_API_SERVER + "weixin/isAttention",
					method: 'POST',
					data: attentionParams
				},
				function(err, data) {

					console.log('是否关注')
					console.log(data)

					resultData.guanzhu = data.guanzhu;

					cb();

				}
			);

		},
		//用户注册
		function(cb) {

			//console.log('开始注册用户')

			//			var infoUrl = "http://192.168.0.8:3400/weixin/Login";
			var infoUrl = config.publicConfig.MIDDLE_API_SERVER + "weixin/Login";
			var tempOpenids = resultData.openIds.openid;
			//			var tempOpenids = resultData.userInfo.openid;
			if(tempOpenids) {
//				tempOpenids = tempOpenids.replace("--", "__");

//				var subStr = "\-{1,1}"; //直接量法创建正则表达式对象,不区分大小写,全局查找
				tempOpenids = tempOpenids.replace(/-/g,"*"); //把'is'替换为空字符串

				//console.log("修改后的openid:" + tempOpenids);
			}
			var sex = 0;

			//为临时登录造数据
			if(linshiOpenid == 'linshi') {
				//创建数据
				resultData.userInfo = {
					'nickname': '',
					'headimgurl': ''
				}
			} else {

				//判断微信返回性别
				if(resultData.userInfo.sex <= 0) {
					sex = 2;
				} else {
					sex = resultData.userInfo.sex - 1;
				}

			}
			
			//去除昵称单引号
			resultData.userInfo.nickname = resultData.userInfo.nickname.replace(/['"".\-\/]/g,""); //把'单引号'替换为空字符串
			
			R.SEND_HTTP(
				req, {
					url: infoUrl,
					method: 'POST',
					data: {
						"openid": tempOpenids,
						"u_username": 1,
						"u_sex": sex, //微信1男2女0保密    本地0男1女2保密
						"u_password": 123456,
						"u_nickname": resultData.userInfo.nickname || '',
						"u_header_url": resultData.userInfo.headimgurl || ''
					}
				},
				function(err, data) {

					console.log('用户注册完成')
					console.log(data)

					//判断是否有错误信息
					if(err) {
						return cb(err, data);
					}

					resultData.userId = data.res;
					resultData.openIds = tempOpenids;
					resultData.nickname = resultData.userInfo.nickname;
					resultData.headimgurl = resultData.userInfo.headimgurl;
					//					resultData.openIds = resultData.userInfo.openid;

					cb();
				}
			);

		}

	], function(err, result) {

		//返回最终拼接结果
		console.log('最终结果。。。。')
		console.log(resultData)

		RES.response(res, true, resultData);

	});

}